<?php
class States extends MY_Controller
{
	var $curl = 'states';
	var $name = '';
	var $search = '';
	var $per_page = 0;
	var $language;
	var $submenu = "_menu";
			
	function __construct()
	{
		parent::__construct();
				
		$this->load->model(array('/public/geo_country_model', '/public/geo_state_model','/public/user_model'));

		$this->validation->set_fields($this->geo_state_model->fields);

		$this->data['Title'] = T_('Области');

		$this->language = parent::getLang();

		$this->name = T_('Области');
	}
	
	function index()
	{
		$this->geo_state_model->search();

		$this->renderLayout();
	}

	function create()
	{
		$this->data['Title'] = $this->name . ' / ' . T_('Create');
		
		if (count($_POST))
		{	
			$rules = array('id_countries', 'name', 'declension', 'capital_name', 'capital_declension');
			$this->validation->set_rules($this->geo_state_model->getRules($rules));

			if ($this->validation->run() === TRUE)
			{
				$item['id_countries'] 		= $this->validation->id_countries;
				$item['name'] 				= $this->validation->name;				
				$item['declension'] 		= $this->validation->declension;
				$item['capital_name'] 		= $this->validation->capital_name;
				$item['capital_declension'] = $this->validation->capital_declension;

				if ($this->geo_state_model->insert($item))
				{
					redirect($this->curl . '/index');	
				}
			}
		}

		$this->renderLayout();		
	}
		
	function edit($id = null)
	{
		if (!empty($id))
		{
			$object = $this->geo_state_model->get($id);
			$this->data['Title'] = $object['name'] . ' / ' . T_('Редактировать');
					
			if (!count($object))
			{
				$this->data['view'] = '_elements/query_no_object';
				$this->load->view($this->data['layout'], $this->data);
	
				return false; 
			}
		
			if (!count($_POST))
			{
				$this->validation->id 			= $object['id'];
				$this->validation->id_countries = $object['id_countries'];  
				$this->validation->name 		= $object['name'];
				$this->validation->declension 	= $object['declension'];
				$this->validation->capital_name = $object['capital_name'];
				$this->validation->capital_declension = $object['capital_declension']; 
			}
			else
			{
				$rules = array('id_countries', 'name', 'declension', 'capital_name', 'capital_declension');
				$this->validation->set_rules($this->geo_state_model->getRules($rules));
	
				if ($this->validation->run() === TRUE)
				{
					$item['id']			 		= $this->validation->id;
					$item['id_countries'] 		= $this->validation->id_countries;
					$item['name'] 				= $this->validation->name;				
					$item['declension'] 		= $this->validation->declension;
					$item['capital_name'] 		= $this->validation->capital_name;
					$item['capital_declension'] = $this->validation->capital_declension;
											
					$this->geo_state_model->update($item);
					
					redirect($this->curl . '/index');				
				}
			}			
		}
		
		$this->renderLayout();
	}

	function deleteItems()
	{
		$data = $this->geo_state_model->delete($_REQUEST['items']);

		echo $data;
	}

	function delete($id)
	{
		$data = $this->geo_state_model->delete($id);

		if ($data)
		{
			redirect($this->curl . '/index');	
		}
		else
		{
			redirect($this->curl . '/edit/'. $id);
		}
	}

	function loadStatesByCountryId($id_countries = null, $wrap_elem = '', $onchange = '')
	{
		$this->layout = 'ajax';
		$this->data['id_countries'] = $id_countries;
		$this->data['wrap_elem']  = $wrap_elem;

		if (!$onchange) 
		{
			$onchange = '';//'loadRegions(this.value)';	
		}
		
		if ($wrap_elem)
		{
			$onchange = 'onchange="'.$onchange.'"';
		}

		$this->data['onchange']  = $onchange;
		$this->load->view('states/ajax/select_states', $this->data);
	}	
		
	/*
	function loadCitiesByRegionId($region_ID = null, $wrap_elem = '')
	{
		$this->layout = 'ajax';
		$this->data['region_ID'] = $region_ID;
		$this->data['wrap_elem']  = $wrap_elem;
		$onchange = '';
		
		if ($wrap_elem == 'place_cities_2')
		{
			//$onchange = 'onchange="loadHotels(this.value, \'place_hotels_2\')"';
		}
		else if ($wrap_elem == 'place_cities')
		{
			$onchange = 'onchange="loadHotels(this.value, \'place_hotels\')"';
		}
		$this->data['onchange']  = $onchange;
		$this->load->view('geo/ajax/select_cities', $this->data);
	}*/	

}
?>